package com.tensquare.article.service;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
import com.tensquare.util.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    public List<Article> findAll() {
        //从缓存拿
        log.info("------------从Redis开始拿取数据---------------");
        List<Article> articles = (List<Article>)this.redisTemplate.boundValueOps("Article.save").get();
        if(CollectionUtils.isEmpty(articles)){
            log.info("------------从Redis拿取数据失败---------------");
            log.info("------------从数据库开始拿取数据---------------");
            List<Article> article = this.articleDao.selectList(null);
            if (CollectionUtils.isEmpty(article)){
                log.info("------------数据库数据异常---------------");
                return null;
            }
            return article;
        }else {
            log.info("------------从Redis拿取数据成功---------------");
            return articles;
        }
    }

    public Article findArticleById(String articleId) {
        Article article = this.articleDao.selectById(articleId);
        return article;
    }

    public void save(Article article) {
        String id = idWorker.nextId() + "";
        article.setId(id);
        //浏览量
        article.setVisits(0);
        //点赞数
        article.setThumbup(0);
        //评论数
        article.setComment(0);
        this.articleDao.insert(article);
        //更新redis缓存数据
        saveToredis();
    }

    public void saveToredis(){
        List<Article> redisData = this.articleDao.selectList(null);
        //存redis
        this.redisTemplate.boundValueOps("Article.save").set(redisData);
    }

    public void updateById(Article article) {
        //根据id修改
        this.articleDao.updateById(article);
        //根据条件修改
        //EntityWrapper<Article> wrapper = new EntityWrapper<>();
        //wrapper.eq("id",article.getId());
        //this.articleDao.update(article, wrapper);
        //更新redis缓存数据
        saveToredis();
    }

    public void deleteById(String articleId) {
        this.articleDao.deleteById(articleId);
        //更新redis缓存数据
        saveToredis();
    }

    public Page<Article> findByPage(Integer page, Integer size, Map<String, Object> map) {
        //查询条件
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (map.get(key) != null){
                wrapper.eq(key,map.get(key));
            }
        }
        //分页参数
        Page<Article> pageData = new Page<>(page,size);
        //查询
        List<Article> list = this.articleDao.selectPage(pageData, wrapper);
        pageData.setRecords(list);
        //返回
        return pageData;
    }
}
